/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xlx.openLink.base.BasePage;
import com.xlx.openLink.base.BaseUtils;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.conf.MailBean;
import com.xlx.openLink.dao.SysMapper;
import com.xlx.openLink.entity.pojo.Backup;
import com.xlx.openLink.entity.pojo.Sys;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.enums.SysParamsEnums;
import com.xlx.openLink.uitls.AssertUtils;
import com.xlx.openLink.vo.MysqlInfo;
import com.xlx.openLink.vo.RequestSystLst;
import com.xlx.openLink.vo.SshVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SysService {


    @Autowired
    private SysMapper sysMapper;


    public Sys selctByCode(String code) {
        return sysMapper.selectByCode(code);
    }

    @Transactional
    public int optionSSH(String code, SshVo sshVo) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), sshVo);
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), sshVo.getIp());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), sshVo.getPwd());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), sshVo.getPort());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), sshVo.getName());
        Sys sys = sysMapper.selectByCode(code);
        if (sys == null) {
            sys = new Sys();
            sys.setSysCode(code);
            sys.setSysParam(JSON.toJSONString(sshVo));
            sys.setPkGuid(BaseUtils.createGUID());
            sys.setSysName(SysParamsEnums.SYS_SSH.getDesc());
            sys.setSysCreateDate(new Date());
        } else {
            sys.setSysUpdateDate(new Date());
            sys.setSysParam(JSON.toJSONString(sshVo));
        }
        sys.setSysUpdateDate(new Date());
        sys.setSysParam(JSON.toJSONString(sshVo));
        return sysMapper.updateByPrimaryKeySelective(sys);
    }

    @Transactional
    public int optionMysqlInfo(String code, MysqlInfo mysqlInfo) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mysqlInfo);
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mysqlInfo.getuName());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mysqlInfo.getPwd());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mysqlInfo.getDbName());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mysqlInfo.getHost());
        Sys sys = sysMapper.selectByCode(code);
        if (sys == null) {
            sys = new Sys();
            sys.setSysCode(code);
            sys.setSysParam(JSON.toJSONString(mysqlInfo));
            sys.setPkGuid(BaseUtils.createGUID());
            sys.setSysName(SysParamsEnums.MYSQL_INFO.getDesc());
            sys.setSysCreateDate(new Date());
        } else {
            sys.setSysUpdateDate(new Date());
            sys.setSysParam(JSON.toJSONString(mysqlInfo));
        }
        return sysMapper.updateByPrimaryKeySelective(sys);
    }


    @Transactional
    public int optionMailBean(String code, MailBean mailBean) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mailBean);
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mailBean.getEmailSMTPHost());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mailBean.getEmailAccount());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mailBean.getEmailPassword());
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK, "参数未空"), mailBean.getProtocol());
        Sys sys = sysMapper.selectByCode(code);
        if (sys == null) {
            sys = new Sys();
            sys.setSysCode(code);
            sys.setSysParam(JSON.toJSONString(mailBean));
            sys.setPkGuid(BaseUtils.createGUID());
            sys.setSysName(SysParamsEnums.SYS_MAIL.getDesc());
            sys.setSysCreateDate(new Date());
        } else {
            sys.setSysUpdateDate(new Date());
            sys.setSysParam(JSON.toJSONString(mailBean));
        }
        return sysMapper.updateByPrimaryKeySelective(sys);
    }


    public BasePage<Sys> listByPage(RequestSystLst request) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK), request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Sys> list = sysMapper.listByPage(request);
        PageInfo pageInfo = new PageInfo(list);
        return new BasePage<Sys>(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }
}
